package Manger.FunctionPkg;

import MySQL_im.getInfo;
import Warning.WarnDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Research extends JFrame implements Win, MouseListener, ActionListener {
    boolean once=true;
    ImageIcon bk_img = new ImageIcon("images/答卷背景.jpg");
    JFrame panel = new JFrame();
    WarnDialog ans;
    ImageIcon next_img = new ImageIcon("images/下一页.png");
    ImageIcon last_img = new ImageIcon("images/上一页.png");
    JLabel bk = new JLabel(bk_img);
    Connection con = new getInfo().getConnection();
    Statement st =null;
    int currentPage =1;
    JLabel title;
    JPanel pan;
    JLabel tips = new JLabel("<html>选项从前到后依次对应：没有（根本不）、很少（有一点）、有时（有些）、经常（相当）、总是（非常）</html>");
    int len=110;
    String q;
    ButtonGroup[] choose;
    JRadioButton[][] scores;
    JLabel[] questions;
    Box[] searchBox;
    JButton certain = new JButton("提交");
    JButton out = new JButton("退出");
    JButton next = new JButton("下一页",next_img);
    JButton last = new JButton("上一页",last_img);
    //        界面初始化
    public Research() {
        once = true;
        q = "SELECT * FROM q0"+currentPage;
        ResultSet re = null;
        try {
            st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re = st.executeQuery(q);
            re.last();
            int max = re.getRow();
            scores = new JRadioButton[max][5];
            questions = new JLabel[max];
            searchBox = new Box[max];
            choose = new ButtonGroup[max];
//            System.out.println(max);
            for (int i = 0; i < max; i++) {
                re.absolute(i);
                re.next();
                searchBox[i] = Box.createVerticalBox();
                questions[i] = new JLabel("<html>"+re.getString(1)+"</html>");
                questions[i].setFont(new Font("楷体",Font.PLAIN,20));
                searchBox[i].add(questions[i]);
                choose[i]=new ButtonGroup();
//                System.out.println(questions[i]);
                for (int j = 0; j < 5; j++) {
                    scores[i][j]=new JRadioButton(re.getString(j+2));
                    choose[i].add(scores[i][j]);
                }
//                searchBox[i].add(ansBox[i]);
                searchBox[i].setBounds(17,len,700,30);
                for (int j = 0; j < 5; j++) {
                    scores[i][j].setBounds(50+j*50,len+32,50,30);
                }
                len += 70;
            }
            next.setVerticalTextPosition(SwingConstants.BOTTOM);
            next.setFont(new Font("黑体",Font.BOLD,12));
            next.setHorizontalTextPosition(SwingConstants.CENTER);
            last.setFont(new Font("黑体",Font.BOLD,12));
            last.setVerticalTextPosition(SwingConstants.BOTTOM);
            last.setHorizontalTextPosition(SwingConstants.CENTER);
            out.addMouseListener(this);
            certain.addMouseListener(this);
        } catch (SQLException e) {
            System.out.println(e);
        }
        title = new JLabel("用户信息展示"+currentPage,SwingConstants.CENTER);
        init();
    }
    //        界面第一次布局
    public void init(){
        panel.setBounds(558,138,735,750);
        panel.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        bk.setSize(bk_img.getIconWidth(),bk_img.getIconHeight());
        panel.getLayeredPane().add(bk,new Integer(Integer.MIN_VALUE));
        pan = (JPanel)panel.getContentPane();
        pan.setOpaque(false);
        pan.setLayout(null);
        pan.add(title);
        title.setFont(new Font("华文行楷",Font.BOLD,30));
        title.setBounds(0,0,735,50);
        pan.add(tips);
        tips.setFont(new Font("黑体",Font.BOLD,20));
        tips.setBounds(22,55,690,50);
        for (int i = 0; i < questions.length; i++) {
            pan.add(searchBox[i]);
            for (int j = 0; j < 5; j++) {
                pan.add(scores[i][j]);
                scores[i][j].setOpaque(false);
            }
        }
        last.setBounds(180,0,60,60);
        next.setBounds(500,0,60,60);
        last.addActionListener(this);
        next.addActionListener(this);
        pan.add(out);
        if(currentPage==9){
            pan.add(certain);
            pan.add(last);
            certain.setBounds(200,len,100,30);
            out.setBounds(400,len,100,30);
        }
        else{
            if(currentPage==1){
                pan.add(next);
            }
            else{
                pan.add(next);
                pan.add(last);
            }
            out.setBounds(300,len,100,30);
        }
        panel.setTitle("数据调查");
        panel.setVisible(true);
        validate();
        ans = new WarnDialog("注意","现在退出的话答卷进度将会清零，您确定退出吗？",panel);
    }
    //    界面更新一页
    public void actionPerformed(ActionEvent e){
        Object obj = e.getSource();
        once = true;
        pan.removeAll();
        len=110;
        if(obj==next){
            currentPage +=1;
        }
        if(obj==last){
            currentPage -= 1;
        }
        title = new JLabel("中医体质测试"+currentPage,SwingConstants.CENTER);
        q = "SELECT * FROM q0"+currentPage;
        ResultSet re = null;
//        更新源数据
        try {
            re = st.executeQuery(q);
            re.last();
            int max = re.getRow();
            scores = new JRadioButton[max][5];
            questions = new JLabel[max];
            searchBox = new Box[max];
            choose = new ButtonGroup[max];
            for (int i = 0; i < max; i++) {
                re.absolute(i);
                re.next();
                searchBox[i] = Box.createVerticalBox();
                questions[i] = new JLabel("<html>"+re.getString(1)+"</html>");
                questions[i].setFont(new Font("楷体",Font.PLAIN,20));
                searchBox[i].add(questions[i]);
                choose[i]=new ButtonGroup();
//                System.out.println(questions[i]);
                for (int j = 0; j < 5; j++) {
                    scores[i][j]=new JRadioButton(re.getString(j+2));
                    choose[i].add(scores[i][j]);
                }
//                searchBox[i].add(ansBox[i]);
                searchBox[i].setBounds(17,len,700,30);
                for (int j = 0; j < 5; j++) {
                    scores[i][j].setBounds(50+j*50,len+32,50,30);
                }
                len += 70;
            }
            next.setVerticalTextPosition(SwingConstants.BOTTOM);
            next.setFont(new Font("黑体",Font.BOLD,12));
            next.setHorizontalTextPosition(SwingConstants.CENTER);
            last.setFont(new Font("黑体",Font.BOLD,12));
            last.setVerticalTextPosition(SwingConstants.BOTTOM);
            last.setHorizontalTextPosition(SwingConstants.CENTER);
            out.addMouseListener(this);
            certain.addMouseListener(this);
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        pan.repaint();
        pan.add(title);
        title.setFont(new Font("华文行楷",Font.BOLD,30));
        title.setBounds(0,0,735,50);
        pan.add(tips);
        for (int i = 0; i < questions.length; i++) {
            pan.add(searchBox[i]);
            for (int j = 0; j < 5; j++) {
                pan.add(scores[i][j]);
                scores[i][j].setOpaque(false);
            }
        }
        pan.add(out);
        if(currentPage==9){
            pan.add(certain);
            pan.add(last);
            certain.setBounds(200,len,100,30);
            out.setBounds(400,len,100,30);
        }
        else{
            if(currentPage==1){
                pan.add(next);
            }
            else{
                pan.add(next);
                pan.add(last);
            }
            out.setBounds(300,len,100,30);
        }
        pan.updateUI();
    }
    //    弹出退出界面，仅弹出一次，便不再弹出
    public void mouseClicked(MouseEvent e){
        Object obj = e.getSource();
        if(obj == out){
            if (once){
                ans.Start();
                once = ans.Ans();
            }
        }
    }
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
}
